package org.example.aop;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(* org.example.service.*.add(..))")
    public void a(){
    }
    @Pointcut("execution(* org.example.service.*.delete(..))")
    public void b(){
    }
    @Pointcut("execution(* org.example.service.*.find*(..))")
    public void c(){
    }

    @AfterReturning(value = "a()||b()||c()",returning = "id")
    public void recordReturnValue(int id){
        System.out.println("===========================");
        System.out.println("添加成功，新产品的id为:"+id);
        System.out.println("===========================");
    }

    @Before("a()||b()||c()")
    public void log(){
        System.out.println("===========================");
        System.out.println("hello, 我是一个前置增强");
        System.out.println("===========================");
    }

    @Before("a()||b()||c()")
    public void time(){
        System.out.println("===========================");
        System.out.println(new Date());
        System.out.println("===========================");
    }

    @After("a()||b()")
    public void after(){
        System.out.println("===========================");
        System.out.println("bye, 我是一个后置增强");
        System.out.println("===========================");
    }
    @Before("a()||b()")
    public void checkRight(){
        System.out.println("===========================");
        System.out.println("权限效验增强");
        Random r=new Random();
        if (r.nextInt(2) == 0) {
            System.out.println("效验成功");
        }else {
            System.out.println("效验失败");
            throw new RuntimeException("用户权限不足...");
        }
        System.out.println("===========================");
    }
}
